﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Media;

namespace Vixen.Framework
{
    /// <summary>Encapsulates variables and methods related to a custom Video handler on the XNA Framework.</summary>
    public class Video
    {
        private readonly VideoPlayer _player; // The video player
        private readonly Microsoft.Xna.Framework.Media.Video _video; // The video itself
        private Texture2D _videoTexture; // The texture of the current video frame

        /// <summary>Creates a video using the passed parameters.</summary>
        /// <param name="assetName">The name of the video asset to load.</param>
        public Video(string assetName)
        {
            _video = Engine.LoadAsset<Microsoft.Xna.Framework.Media.Video>(assetName);
            _player = new VideoPlayer();
        }

        /// <summary>Checks if the video is currently stopped or not.</summary>
        public bool Stopped
        {
            get
            {
                if (_player.State != MediaState.Stopped)
                    return false;
                return true;
            }
        }

        /// <summary>Plays the video.</summary>
        public void Play()
        {
            if (_player.State != MediaState.Playing)
                _player.Play(_video);
        }

        /// <summary>Draws the frame of the video to the screen.</summary>
        /// <param name="spriteBatch">The SpriteBatch used to draw the Sprite to the screen.</param>
        public void Draw(SpriteBatch spriteBatch)
        {
            if (!Stopped)
                _videoTexture = _player.GetTexture();

            if (_videoTexture != null)
                spriteBatch.Draw(_videoTexture, Engine.Screen.Area, Color.White);
        }
    }
}